package com.example.common.component;

import com.example.common.control.ICallback;
import com.example.common.model.Category;
import com.example.common.model.GameApiRankResp;
import com.example.common.model.MyResponse;
import com.example.common.network.CategroyReq;
import com.example.common.network.GameReq;
import com.example.common.network.RetrofitServiceManager;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CategoryCtrlCom {
    private CategroyReq categroyReq;

    public CategoryCtrlCom() {
        categroyReq = RetrofitServiceManager.getInstance().create(CategroyReq.class);
    }

    //获取分类列表
    public void getCategory(ICallback iCallback) {
        //创建请求
        Call<MyResponse<List<Category>>> call = categroyReq.getCategory();
        //创建请求
        call.enqueue(new Callback<MyResponse<List<Category>>>() {
            @Override
            public void onResponse(Call<MyResponse<List<Category>>> call, Response<MyResponse<List<Category>>> response) {
                if (response.isSuccessful()) {
                    iCallback.onSuccess(response.body().getData());
                } else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<List<Category>>> call, Throwable t) {
                iCallback.onFailed("network error");

            }
        });
    }

    /**
     * 获取分类下的游戏
     *
     * @param pn        页码, 默认为1
     * @param limit     每页显示的个数, 取值范围为15到30, 默认为30
     * @param iCallback 返回类型 GameApiRankResp
     */
    public void getCategoryGames(Integer id, Integer pn, Integer limit, ICallback iCallback) {
        Call<MyResponse<GameApiRankResp>> call = categroyReq.getCategoryGames(id, pn, limit);
        call.enqueue(new Callback<MyResponse<GameApiRankResp>>() {
            @Override
            public void onResponse(Call<MyResponse<GameApiRankResp>> call, Response<MyResponse<GameApiRankResp>> response) {
                if (response.isSuccessful()) {
                    MyResponse<GameApiRankResp> myResponse = response.body();
                    if (myResponse != null) {
                        if (myResponse.getCode() == 200) {
                            iCallback.onSuccess(myResponse.getData());
                        } else {
                            iCallback.onFailed(myResponse.getErrmsg());
                        }
                    } else {
                        iCallback.onFailed("返回体为空");
                    }
                } else {
                    iCallback.onFailed("错误码500");
                }
            }

            @Override
            public void onFailure(Call<MyResponse<GameApiRankResp>> call, Throwable t) {
                iCallback.onFailed("网络错误");
            }
        });
    }
}
